package cn.luo.dao;

import cn.luo.entity.Blogs;
import cn.luo.entity.Content;
import cn.luo.entity.Tag;
import cn.luo.uilt.SessionUitls;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public class BlogsDao {
    @Autowired
    MongoTemplate mongoTemplate;

    //添加标题
    public void addTitle(Blogs blogs){
        mongoTemplate.save(blogs);
    }


    //博文分页查询
    public List<Blogs> findByPage(int start,int num){
        Query query = new Query();
        query.skip(start).limit(num);
        List<Blogs> lst = mongoTemplate.find(query,Blogs.class);
        return lst;
    }

    //计算总数
    public long total(){
        Query query = new Query();
        long count = mongoTemplate.count(query, Blogs.class);
        return count;
    }

    //内容表操作
    //添加内容-写博文
    public void addboke(Content content){
        mongoTemplate.save(content);
    }

    //根据博文ID查找单个
    public Blogs findByBlogsId(String id){
        Query query = new Query(Criteria.where("_id").is(id));
        Blogs blogs = mongoTemplate.findOne(query,Blogs.class);
        return blogs;
    }

    //博文分页查询
    public List<Blogs> findByPageByTagId(int start,int num,String tagid){
        Query query = new Query();
        if (!tagid.equals("")){
            Criteria criteria = new Criteria();
            criteria.and("tagid").is(tagid);
            query.addCriteria(criteria);
        }
        query.skip(start).limit(num);
        List<Blogs> lst = mongoTemplate.find(query,Blogs.class);
        return lst;
    }

    //计算总数
    public long tagtotal(String tagid){
        Query query = new Query();
        if (! tagid.equals("")){
            Criteria criteria = new Criteria();
            criteria.and("tagid").is(tagid);
            query.addCriteria(criteria);
        }
        long count = mongoTemplate.count(query, Blogs.class);
        return count;
    }
}
